package Engine.ParticelSystem;



import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex2f;

import java.util.Random;

import org.lwjgl.Sys;
import org.lwjgl.opengl.GLContext;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;


import Engine.Time;

public class Particle {

	private ParticleConfiguration particelconfig;
	private int xPosition;
	private int yPosition;
	private float randomSpeedMul=2;
	Color color;
	private float lifetime;
	Random random;
	private boolean Render = false;
	private float randomRotation;
	public Particle(ParticleConfiguration partconfig) {
		particelconfig = partconfig;
		random = new Random();
		color = Color.orange;
		//randomSpeedMul = (random.nextFloat()*0.5f+1);
		randomRotation = (random.nextFloat()-0.5f)*partconfig.getRandomRotation();
	}
	
	public void Draw(Time time){
		if(random.nextInt((int) (particelconfig.getParticleCount()*3*particelconfig.getLifetime()))==1)
			Render  = true;
		if(Render){
			
			yPosition += time.getDelta()*particelconfig.getYForce()*randomSpeedMul;
			xPosition += time.getDelta()*particelconfig.getXForce()*randomSpeedMul;
			particelconfig.getTexture().bind();
			lifetime += time.getDelta();
			
			if(lifetime>particelconfig.getLifetime()*10){
				xPosition = 0;
				yPosition = 0;
				lifetime = 0;

			}
				
			color =particelconfig.color(lifetime);
			glPushMatrix();
				glRotatef(randomRotation, 0, 0, 1);
				glTranslatef(xPosition, yPosition, -particelconfig.getZindex());
				glTranslatef(-0.5f*particelconfig.getSize(lifetime), -0.5f*particelconfig.getSize(lifetime), 0);
				glBegin(GL_QUADS);
					glTexCoord2f(0,0);
					glColor4f(color.r, color.g, color.b, color.a);
					glVertex2f(0,0);
					
					glTexCoord2f(1,0);
					glColor4f(color.r, color.g, color.b, color.a);
					glVertex2f(particelconfig.getSize(lifetime),0);
					
					glTexCoord2f(1,1);
					glColor4f(color.r, color.g, color.b, color.a);
					glVertex2f(particelconfig.getSize(lifetime),particelconfig.getSize(lifetime));
					
					glTexCoord2f(0,1);
					glColor4f(color.r, color.g, color.b, color.a);
					glVertex2f(0,particelconfig.getSize(lifetime));
					glColor4f(1, 1, 1, 1);
				glEnd();
			glPopMatrix();
		}
    }
	public int getxPosition() {
		return xPosition;
	}

	public void setxPosition(int xPosition) {
		this.xPosition = xPosition;
	}

	public int getyPosition() {
		return yPosition;
	}

	public void setyPosition(int yPosition) {
		this.yPosition = yPosition;
	}



}
